﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using HOPU.OnlineSchool.Application.Subject.Dto;
using HOPU.OnlineSchool.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HOPU.OnlineSchool.Application.Subject.Services
{
    public class SubjectService : ISubjectService, ITransient
    {
        private readonly IRepository<EduSubject> _repository;
        public SubjectService(IRepository<EduSubject> repository)
        {
            _repository = repository;
        }
        public async Task BatchAddAsync(IFormFile file)
        {
            //using (var fs = file.OpenReadStream())
            //{
            //    IWorkbook workbook = WorkbookFactory.Create(fs);
            //    var sheet = workbook.GetSheetAt(0);
            //    int rowCount = sheet.LastRowNum;

            //    //待导入的分类集合
            //    var importList = new List<EduSubject>();

            //    //获取当前系统中的课程分类
            //    var list = await _repository.AsQueryable().ToListAsync();

            //    for (int i = 1; i <= rowCount; ++i)
            //    {
            //        IRow row = sheet.GetRow(i);
            //        int cellCount = row.LastCellNum;

            //        var dto = new ExcelSubjectDto();
            //        EduSubject oneLevelSubject = null;

            //        //读取一级分类
            //        dto.OneLevelName = row.GetCell(0).StringCellValue;

            //        //判断当前一级分类是否已经存在
            //        if (!IsSubjectExists(list, dto.OneLevelName, 1))
            //        {
            //            oneLevelSubject = new EduSubject() { Id = YitIdHelper.NextId().ToString(), ParentId = "0", Title = dto.OneLevelName };

            //            //添加新类别
            //            var newOneSubject = oneLevelSubject;

            //            //加入到当前已存在分类集合中
            //            list.Add(newOneSubject);

            //            //加入到待导入的分类集合中
            //            importList.Add(newOneSubject);
            //        }
            //        else
            //        {
            //            oneLevelSubject = list.FirstOrDefault(e => e.Title == dto.OneLevelName && e.ParentId == "0");
            //        }

            //        //读取二级分类
            //        dto.TwoLevelName = row.GetCell(1).StringCellValue;

            //        //判断当前二级分类是否已经存在
            //        if (!IsSubjectExists(list, dto.TwoLevelName, 2))
            //        {
            //            //添加新类别
            //            var newTwoSubject = new EduSubject() { Id = YitIdHelper.NextId().ToString(), ParentId = oneLevelSubject.Id, Title = dto.TwoLevelName };

            //            //加入到当前已存在分类集合中
            //            list.Add(newTwoSubject);

            //            //加入到待导入的分类集合中
            //            importList.Add(newTwoSubject);
            //        }
            //    }

            //    //批量写入数据库
            //    await _repository.InsertAsync(importList);
            //}

        }

        public async Task<List<SubjectTreeDto>> GetTreeListAsync()
        {
            var list = await _repository.Entities.AsNoTracking().ToListAsync();
            var oneList = list.Where(e => e.ParentId == "0");
            var treeList = new List<SubjectTreeDto>();
            foreach (var item in oneList)
            {
                var treeDto = new SubjectTreeDto() { Id = item.Id, Title = item.Title };
                var children = list.Where(e => e.ParentId == item.Id).Select(e => new SubjectDto() { Id = e.Id, ParentId = e.ParentId, Title = e.Title }).ToList();
                treeDto.Children = children;
                treeList.Add(treeDto);
            }
            return treeList;
        }
    }
}
